/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;

/**
 *
 * @author patricksamson236
 */
public class DB_Read {

    private ArrayList<Client> arClient;
    private ArrayList<Employe> arEmploye;
    private ArrayList<Entree> arEntree;
    private ArrayList<Facture> arFactures;
    private String versionBD;
    private File fichier;
    private FileInputStream fis;
    private GZIPInputStream gis;
    private ObjectInputStream ois;
    private BufferedInputStream bis;
    private ReadableByteChannel rbc;
    private RandomAccessFile raf;
    private FileChannel in;

    public DB_Read(String versionBD) throws IOException, ClassNotFoundException {
        arEmploye = new ArrayList<Employe>();
        arClient = new ArrayList<Client>();
        arEntree = new ArrayList<Entree>();
        arFactures = new ArrayList<Facture>();
        this.versionBD = versionBD;
        fichier = new File("BaseDonnées2.dat");  //"C:\\Programmation\\BaseDonnées.dat"

        if (!fichier.exists()) {
            fichier.createNewFile();
            new DB_Write(arClient, arEmploye, arEntree, arFactures, versionBD);
        } else {
            fis = new FileInputStream(fichier);
            gis = new GZIPInputStream(fis);
            ois = new ObjectInputStream(gis);

            if (ois.available() == 1) {
                versionBD = (String) ois.readObject();
            } else {
                //.V0.1
                //String temp = (String) ois.readObject();
                //V.0.2
                String temp = "" + (char) ois.read() + (char) ois.read();
                if (temp.equals("01")) {
                    System.out.println("Reading DB V.0.1");
                    version01();
                } else if (temp.equals("02")) {
                    //System.out.println("Reading DB V.0.2");
                    //version02();
                }
            }
        }
        version02();
        //new DB_Write(arClient, arEmploye, arEntree, arFactures, versionBD);
        //version01();
    }

    public ArrayList<Client> getArClient() {
        return arClient;
    }

    public ArrayList<Employe> getArEmploye() {
        return arEmploye;
    }

    public ArrayList<Entree> getArEntree() {
        return arEntree;
    }

    public ArrayList<Facture> getArFactures() {
        return arFactures;
    }

    public void version01() throws IOException, ClassNotFoundException {
        fichier = new File("BaseDonnéesReference.dat");
        fis = new FileInputStream(fichier);
        gis = new GZIPInputStream(fis);
        ois = new ObjectInputStream(gis);

        long a = System.nanoTime();
        String s = (String) ois.readObject();
        ArrayList t = (ArrayList<Employe>) ois.readObject();
        t = (ArrayList<Client>) ois.readObject();
        t = (ArrayList<Entree>) ois.readObject();
        t = (ArrayList<Facture>) ois.readObject();

        ois.close();
        long b = System.nanoTime();
        System.out.println("old time Read = " + (b - a) + "ns --> " + ((double) (b - a)) / 1000000000);

        /*
         * arEmploye.get(0).setTache(Travail_Effectue.VERF);
         * System.out.println(arEmploye.size() + " Employé(s)");
         * System.out.println(arClient.size() + " Client(s)");
         * System.out.println(arEntree.size() + " Entrée(s)");
         * System.out.println(arFactures.size() + " Facture(s)"); new
         * DB_Write(arClient, arEmploye, arEntree, arFactures, versionBD);
         */
    }

    public void version02() throws IOException {
        fichier = new File("BaseDonnées2.dat");
        fis = new FileInputStream(fichier);
        gis = new GZIPInputStream(fis);
        ois = new ObjectInputStream(gis);
        int count;
        int length;
        byte[] buf;
        String[] temp;

        long a = System.nanoTime();
        //on lit la version de la DB
        versionBD = "" + (char) ois.read() + (char) ois.read();

        //on lit les employes
        count = ois.readInt();
        for (int i = 0; i < count; i++) {
            length = ois.readShort();
            buf = new byte[length];
            ois.readFully(buf);
            temp = new String(buf).split("~");
            arEmploye.add(new Employe(temp[0], temp[1], temp[2],
                    Double.parseDouble(temp[3]), new Boolean(temp[4]),
                    Travail_Effectue.valueOf(temp[5])));
        }

        //on lit les clients
        count = ois.readInt();
        for (int i = 0; i < count; i++) {
            length = ois.readShort();
            buf = new byte[length];
            ois.readFully(buf);
            temp = new String(buf).split("~");
            arClient.add(new Client(temp[0], temp[1]));
        }

        //on lit les entrées
        count = ois.readInt();
        for (int i = 0; i < count; i++) {
            length = ois.readShort();
            buf = new byte[length];
            ois.readFully(buf);
            temp = new String(buf).split("~");
            arEntree.add(new Entree(Integer.parseInt(temp[0]),
                    Integer.parseInt(temp[1]),
                    Integer.parseInt(temp[2]),
                    arEmploye.get(Integer.parseInt(temp[3])),
                    arClient.get(Integer.parseInt(temp[4])),
                    Double.parseDouble(temp[5]),
                    Double.parseDouble(temp[6]),
                    Travail_Effectue.valueOf(temp[7])));
            arEntree.get(i).setFacturee(Boolean.valueOf(temp[8]));
        }

        //on lit les factures
        count = ois.readInt();
        for (int i = 0; i < count; i++) {
            length = ois.readShort();
            buf = new byte[length];
            ois.readFully(buf);
            temp = new String(buf).split("~");
            arFactures.add(new Facture(arClient.get(Integer.parseInt(temp[0]))));
            //(temp[1]) -- no date can be set!
            arFactures.get(i).setMontantCharge(Double.parseDouble(temp[2]));
            arFactures.get(i).setNumero(temp[3]);

            //on lit la liste d'entrées de la facuture
            int nbe = ois.readInt();
            for (int j = 0; j < nbe; j++) {
                int k = ois.readInt();
                arFactures.get(i).addEntree(arEntree.get(k));
            }
        }

        long b = System.nanoTime();
        System.out.println("new time Read = " + (b - a) + "ns --> " + ((double) (b - a)) / 1000000000);
        System.out.println(arEmploye.size() + " Employé(s)");
        System.out.println(arClient.size() + " Client(s)");
        System.out.println(arEntree.size() + " Entrée(s)");
        System.out.println(arFactures.size() + " Facture(s)");
    }
}
